#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import pytest
from utils import db
from locators.loginpage import LoginPage
from locators.mainforcepage import MainForcePage
from locators.errorpage import ErrorPage
from locators.calendar import Calendar
from locators.navibar import NaviBar
from locators.alert import Alert
from locators.earlytradepage import EarlyTradePage
from locators.emotioncyclepage import EmotionCyclePage
from locators.stocksearchpage import StockSearchPage
from locators.stockdetailpage import StockDetailPage
from locators.stocktrackpage import StockTrackPage
from locators.platepage import PlatePage
from locators.subscribepage import SubscribePage
from locators.personalpage import PersionalPage
from locators.stocktrackpage4 import StockTrackPage4
from locators.earlytradepage4 import EarlyTradePage4


conn=None
@pytest.fixture(scope='session')
def mysql():
    global conn
    if conn is None:
        conn=db.Mysql()
    return conn

@pytest.fixture(scope='session')
def loginPage():
    return LoginPage

@pytest.fixture(scope='session')
def alert():
    return Alert

@pytest.fixture(scope='session')
def naviBar():
    return NaviBar

@pytest.fixture(scope='session')
def mainForcePage():
    return MainForcePage

@pytest.fixture(scope='session')
def earlyTradePage():
    return EarlyTradePage

@pytest.fixture(scope='session')
def errorPage():
    return ErrorPage

@pytest.fixture(scope='session')
def calendar():
    return Calendar

@pytest.fixture(scope='session')
def stockDetailPage():
    return StockDetailPage

@pytest.fixture(scope='session')
def emotionCyclePage():
    return EmotionCyclePage

@pytest.fixture(scope='session')
def stockSearchPage():
    return StockSearchPage

@pytest.fixture(scope='session')
def stockTrackPage():
    return StockTrackPage

@pytest.fixture(scope='session')
def platePage():
    return PlatePage

@pytest.fixture(scope='session')
def subscribePage():
    return SubscribePage

@pytest.fixture(scope='session')
def persionalPage():
    return PersionalPage

@pytest.fixture(scope='session')
def stockTrackPage4():
    return StockTrackPage4

@pytest.fixture(scope='session')
def earlyTradePage4():
    return EarlyTradePage4

@pytest.fixture
def newUser(mysql):
    phone='15828568760'
    yield phone
    sql = 'delete from site.sl_site_user where phone=%s'
    mysql.delete(sql, [phone])

@pytest.fixture
def get_code(mysql):
    def wrapper(phone):
        sql='select code from la_sms_log where mobile=%s order by id desc limit 1'
        return mysql.query(sql,[phone])[0]['code']
    return wrapper

@pytest.fixture
def get_auth_login_url(py_config,api):
    conf=py_config.custom['env']
    def wrapper(appid:str=conf['appid'],unionid:str=conf['unionId'],courseNo:str=conf['courseNo'] ):
        url = conf['base_url'] + '/api/data/site/create_auto_url'
        data = {
            "app_id": appid,
            "course_no": courseNo,
            "unionid": unionid
        }
        result = api.post(url, json=data)
        return result.json()['result']['data']['url']
    return wrapper

@pytest.fixture
def skip_if_prd(pyd):
    if 'com' in pyd.env['base_url']:
        pytest.skip('线上不执行此用例')